if [ -d "/root/Miao-Yunzai" ]; then
    LS_DIR='/root/Miao-Yunzai'
elif [ -d "/root/Yunzai-Bot" ]; then
    LS_DIR='/root/Yunzai-Bot'
elif [ -d "/root/yxybot" ]; then
    LS_DIR='/root/yxybot'
else
    echo "您未安装机器人"
    exit 1
fi

cd $HOME

XRK_DIR='/root/xrk'
PLUGINS_DIR="$LS_DIR/plugins"

if [ ! -d "$PLUGINS_DIR/xiaoyue-plugin" ]; then
    cd $LS_DIR
    git clone --depth=1 https://gitee.com/yunxiyuan/xiaoyue-plugin.git ./plugins/xiaoyue-plugin
    cd ~
fi
if ! grep -q "cat .ml" "$HOME/.profile"; then
    sed -i '/cat .yzml/d' "$HOME/.profile"
    sed -i '9a cat .ml' "$HOME/.profile"
    sed -i '10!s/cat .ml//' "$HOME/.profile"
fi
YZ_SCRIPT="/usr/bin/yz"
if [ -f "$YZ_SCRIPT" ]; then
    rm /usr/bin/yz
fi
    echo '#!/bin/bash' > "$YZ_SCRIPT"
    echo 'echo 正在启动Yunzai-Bot' >> "$YZ_SCRIPT"
    echo "cd $LS_DIR" >> "$YZ_SCRIPT"
    echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$YZ_SCRIPT"
    echo 'node app' >> "$YZ_SCRIPT"
    chmod +x "$YZ_SCRIPT"
YZLOG_SCRIPT="/usr/bin/yzlog"
if [ -f "$YZLOG_SCRIPT" ]; then
    rm /usr/bin/yzlog
fi
    echo '#!/bin/bash' > "$YZLOG_SCRIPT"
    echo 'echo 正在打开Yunzai-Bot后台日志' >> "$YZLOG_SCRIPT"
    echo "cd $LS_DIR" >> "$YZLOG_SCRIPT"
    echo 'pnpm run log' >> "$YZLOG_SCRIPT"
    chmod +x "$YZLOG_SCRIPT"
YZLOGIN_SCRIPT="/usr/bin/yzlogin"
if [ -f "$YZLOGIN_SCRIPT" ]; then
    rm /usr/bin/yzlogin
fi
    echo '#!/bin/bash' > "$YZLOGIN_SCRIPT"
    echo 'echo 正在打开Yunzai-Bot登录配置' >> "$YZLOGIN_SCRIPT"
    echo "cd $LS_DIR" >> "$YZLOGIN_SCRIPT"
    echo 'redis-server --daemonize yes --save 900 1 --save 300 10' >> "$YZLOGIN_SCRIPT"
    echo 'pnpm run login' >> "$YZLOGIN_SCRIPT"
    chmod +x "$YZLOGIN_SCRIPT"
YZSTOP_SCRIPT="/usr/bin/yzstop"
if [ -f "$YZSTOP_SCRIPT" ]; then
    rm /usr/bin/yzstop
fi
    echo '#!/bin/bash' > "$YZSTOP_SCRIPT"
    echo 'echo 正在停止Yunzai-Bot运行' >> "$YZSTOP_SCRIPT"
    echo "cd $LS_DIR" >> "$YZSTOP_SCRIPT"
    echo 'pnpm stop' >> "$YZSTOP_SCRIPT"
    chmod +x "$YZSTOP_SCRIPT"
XRK_SCRIPT="/usr/bin/xrk"
if [ -f "$XRK_SCRIPT" ]; then
    rm /usr/bin/xrk
fi
    echo '#!/bin/bash' > "$XRK_SCRIPT"
    echo 'echo 正在打开向日葵脚本' >> "$XRK_SCRIPT"
    echo "bash <(curl -sL gitee.com/xrk114514/shell-Yunzai/raw/master/shell-all/ybjb.sh)" >> "$XRK_SCRIPT"
    chmod +x "$XRK_SCRIPT"
ML_FILE=".ml"
COMMAND_LIST=(
    "笨比启动云崽命令为 yz"
    "后台查看日志命令为 yzlog"
    "重新配置账号命令为 yzlogin"
    "停止云崽运行命令为 yzstop"
    "启动向日葵脚本命令为 xrk"
)
if [ -f $ML_FILE ]; then
    rm /root/.ml
fi
if [ ! -f $ML_FILE ]; then
    for cmd in "${COMMAND_LIST[@]}" ; do
        key=${cmd% *}
        value=${cmd##* }
        echo "$key $value" >> $ML_FILE
    done
fi

echo "启动命令写入成功"